<?php

//ADD 
//   define('COOKIEPATH', '/'); 
//in wp.config.php


if (!isset($website) ) { header('HTTP/1.1 404 Not Found'); die; }

$wp_load = '1';

if ( !is_logged() ) {

   if ( file_exists( $wp_path.'wp-load.php' ) ) require( $wp_path.'wp-load.php' );

   
   global $current_user;
   $current_user = wp_get_current_user();
   $WP_ID = $current_user->ID;
   $WP_username = $current_user->user_login;
   $WP_user_email = $current_user->user_email;
      
   require_once('inc/common.php');
   require_once('inc/class.database.php');
   require_once('inc/db_connect.php');
   
   $check = $db->query("SELECT * FROM users 
	WHERE user_email = '".$WP_user_email."' AND other_id = '".$WP_ID."'");

   
   		if ( $db->num_rows($check)<=0 ) {
	    //CREATE NEW USER (from phpbb database)
	    $checkUn = $db->query("SELECT * FROM users WHERE LOWER(user_name) = LOWER('".$WP_username."') ");
	    if ( $db->num_rows($checkUn)>=1 ) {
	    $WP_username = $WP_username."_".$WP_ID;
		$pass = generate_hash(5);
		$hash = generate_hash(12);
		$password_db = generate_password($pass, $hash); 
		
		$insert = $db->query("INSERT INTO users(user_name, user_email, user_password, password_hash, user_joined, user_level, user_last_login, user_ip, user_avatar, other_id, user_website )
	   VALUES('".$WP_username."', '".$WP_user_email."', '".$password_db."', '".$hash."', '".(int) time()."', '0', '".(int) time()."', '".safeEscape($_SERVER["REMOTE_ADDR"])."', '', '".$WP_ID."', '' )");
	   $id = $db->get_insert_id();
	   $_SESSION["user_id"] = $id ;
	   $_SESSION["username"] =$WP_username;
	   $_SESSION["email"]    = $WP_user_email;
	   $_SESSION["level"]    = 0;
	   $_SESSION["can_comment"]    = 1;
	   $_SESSION["logged"]    = time();
	   $_SESSION["wp"]    = $WP_ID;
	   $logout = wp_logout_url();
	   $_SESSION["logout"]    = $logout;
	    }
		
	   }
	   else {
	  //UPDATE USER DATA
	  $row = $db->fetch_array($check,'assoc');
	  
	  $update = $db->query("UPDATE users SET 
	  user_last_login = '".(int) time()."' WHERE user_id = '".$row["user_id"]."' LIMIT 1");
	   $_SESSION["user_id"] = $row["user_id"] ;
	   $_SESSION["username"] = $row["user_name"];
	   $_SESSION["email"]    = $WP_user_email;
	   $_SESSION["level"]    = $row["user_level"];
	   $_SESSION["can_comment"]    = 1;
	   $_SESSION["logged"]    = time();
	   $_SESSION["smf"]    = $WP_ID;
	   $logout = wp_logout_url();
	   $_SESSION["logout"]    = $logout;
	  }


}
?>